package com.wgts.wgts.mvp.view.activity.moremore;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.dialog.JoinPeoplesDialog;
import com.wgts.wgts.control.eventbus.OrderFragmentEvenBus;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerMoreMoreAffirmOrderActivityComponent;
import com.wgts.wgts.dagger.module.MoreMoreAffirmOrderActivityModule;
import com.wgts.wgts.localbean.JoinPeopleData;
import com.wgts.wgts.mvp.presenter.IMoreMoreAffirmOrderActivityPresenter;
import com.wgts.wgts.mvp.view.IMoreMoreAffirmOrderActivityView;
import com.wgts.wgts.mvp.view.activity.mycenter.ShippingAddressActivity;
import com.wgts.wgts.netmodle.resp.AwardWinner;
import com.wgts.wgts.netmodle.resp.ShippingAddressEntity;
import com.wgts.wgts.tools.MoHuGlide;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/*
 *
 *
 *   data:2018/9/715:43
 *
 */public class MoreMoreAffirmOrderActivity extends BaseActivity implements IMoreMoreAffirmOrderActivityView {


    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.affirm_location_image)
    ImageView affirmLocationImage;
    @BindView(R.id.choose_adress_ly)
    LinearLayout chooseAdressLy;
    @BindView(R.id.affirm_user_name)
    TextView affirmUserName;
    @BindView(R.id.affirm_user_phone)
    TextView affirmUserPhone;
    @BindView(R.id.affirm_user_location)
    TextView affirmUserLocation;
    @BindView(R.id.show_adress_ly)
    LinearLayout showAdressLy;
    @BindView(R.id.affirm_choose_location)
    LinearLayout affirmChooseLocation;
    @BindView(R.id.shopping_car_item_image)
    ImageView shoppingCarItemImage;
    @BindView(R.id.shopping_car_title)
    TextView shoppingCarTitle;
    @BindView(R.id.shopping_car_type_show)
    TextView shoppingCarTypeShow;
    @BindView(R.id.shopping_car_price)
    TextView shoppingCarPrice;
    @BindView(R.id.order_shop_number)
    TextView orderShopNumber;
    @BindView(R.id.shopping_car_details)
    LinearLayout shoppingCarDetails;
    @BindView(R.id.leave_message_et)
    EditText leaveMessageEt;
    @BindView(R.id.img_lottery_user)
    CircleImageView imgLotteryUser;
    @BindView(R.id.tv_lottery_number)
    TextView tvLotteryNumber;
    @BindView(R.id.tv_guess_price)
    TextView tvGuessPrice;
    @BindView(R.id.linear_lottery)
    LinearLayout linearLottery;
    @BindView(R.id.linear_friend_more)
    LinearLayout linearFriendMore;
    @BindView(R.id.circle_friend_img1)
    CircleImageView circleFriendImg1;
    @BindView(R.id.circle_friend_img2)
    CircleImageView circleFriendImg2;
    @BindView(R.id.circle_friend_img3)
    CircleImageView circleFriendImg3;
    @BindView(R.id.circle_friend_img4)
    CircleImageView circleFriendImg4;
    @BindView(R.id.circle_friend_img5)
    CircleImageView circleFriendImg5;
    @BindView(R.id.circle_friend_img6)
    CircleImageView circleFriendImg6;
    @BindView(R.id.linear_join_friend)
    LinearLayout linearJoinFriend;
    @BindView(R.id.linear_ensure)
    LinearLayout linearEnsure;
    @BindView(R.id.linear_guess_price)
    LinearLayout linearGuessPrice;
    private int AddressResult = 1;
    //选则收货地址数据
    private ShippingAddressEntity resultEntity;
    //收货地址ID
    private String AddressId;

    @Inject
    IMoreMoreAffirmOrderActivityPresenter presenter;

    public static final String WINNID = "WINNID";

    private Integer winnId;

    private JoinPeoplesDialog joinPeoplesDialog;

    public static final String TYPE = "type";

    public static final String MORE_MORE = "MORE_MORE";

    public static final String GUESS = "GUESS";

    private String type = "";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_more_affirm_order_activity);
        ButterKnife.bind(this);
        includeTitle.setText("确认订单");
        initView();
    }

    @Override
    public void init() {
        DaggerMoreMoreAffirmOrderActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .moreMoreAffirmOrderActivityModule(new MoreMoreAffirmOrderActivityModule(this))
                .build()
                .inject(this);
        winnId = Integer.valueOf(getIntent().getStringExtra(WINNID));
        type = getIntent().getStringExtra(TYPE);
    }

    private void initView() {
        presenter.awardWinner(winnId);

        if(type.equals(MORE_MORE)){
            linearGuessPrice.setVisibility(View.INVISIBLE);
        }else{
            linearGuessPrice.setVisibility(View.VISIBLE);
        }

    }

    @OnClick({R.id.linear_ensure, R.id.affirm_choose_location, R.id.linear_friend_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linear_ensure:
                if (AddressId == null || AddressId.equals("")) {
                    MoHuToast.showToast("请选择收货地址");
                    return;
                }
                presenter.awardEnsure(winnId, AddressId);
                break;
            case R.id.affirm_choose_location:
                Intent intent = new Intent(MoreMoreAffirmOrderActivity.this, ShippingAddressActivity.class);
                intent.putExtra(ShippingAddressActivity.AFFIRM_ORDER_ADDRESS_RESULT, "affirmOrderAddressResult");
                startActivityForResult(intent, AddressResult);
                break;
            case R.id.linear_friend_more:
                if (joinPeoplesDialog != null) {
                    joinPeoplesDialog.show(getSupportFragmentManager(), "");
                }
                break;

        }
    }


    @Override
    public void onError(int status, String msg) {
        MoHuToast.showToast(msg);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AddressResult && data != null) {
            resultEntity = data.getParcelableExtra("address");
            chooseAdressLy.setVisibility(View.GONE);
            showAdressLy.setVisibility(View.VISIBLE);
            affirmUserName.setText(resultEntity.userName);
            affirmUserPhone.setText(resultEntity.userPhone);
            affirmUserLocation.setText(resultEntity.province + " " + resultEntity.city + " " + resultEntity.area
                    + " " + resultEntity.details);
            AddressId = resultEntity.addressId;
        }
    }

    @Override
    public void awardWinner(AwardWinner winner) {
        MoHuGlide.getInstance().glideImg(this, winner.orderProduct.productSmallImage, shoppingCarItemImage);
        shoppingCarTitle.setText(winner.orderProduct.productName);
        shoppingCarPrice.setText(winner.orderProduct.productPrice);
        orderShopNumber.setText(winner.orderProduct.drawNumber + "人场次");
        MoHuGlide.getInstance().glideImg(this, winner.winningUserAvatar, imgLotteryUser);
        tvLotteryNumber.setText(winner.winningUserNickName);
        tvGuessPrice.setText(winner.winningPrice);

        if (winner.participateUser.size() > 5) {
            circleFriendImg6.setVisibility(View.VISIBLE);
        }

        for (int i = 0; i < winner.participateUser.size(); i++) {
            switch (i) {
                case 0:
                    MoHuGlide.getInstance().glideImg(this, winner.participateUser.get(i).userImage, circleFriendImg1);
                    circleFriendImg1.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    MoHuGlide.getInstance().glideImg(this, winner.participateUser.get(i).userImage, circleFriendImg2);
                    circleFriendImg2.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    MoHuGlide.getInstance().glideImg(this, winner.participateUser.get(i).userImage, circleFriendImg3);
                    circleFriendImg3.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    MoHuGlide.getInstance().glideImg(this, winner.participateUser.get(i).userImage, circleFriendImg4);
                    circleFriendImg4.setVisibility(View.VISIBLE);
                    break;
                case 4:
                    MoHuGlide.getInstance().glideImg(this, winner.participateUser.get(i).userImage, circleFriendImg5);
                    circleFriendImg5.setVisibility(View.VISIBLE);
                    break;
            }
        }

        ArrayList<JoinPeopleData> joinPeopleDatas = new ArrayList<>();

        for (AwardWinner.ParticipateUser item : winner.participateUser) {
            JoinPeopleData joinPeopleData = new JoinPeopleData();
            joinPeopleData.name = item.userNickName;
            joinPeopleData.price = String.valueOf(item.winnerNumber);
            switch (item.winningState) {
                case "1":
                    joinPeopleData.status = "";
                    break;
                case "2":
                    joinPeopleData.status = "";
                    break;
                case "3":
                    joinPeopleData.status = "已竞中";
                    break;
                case "4":
                    joinPeopleData.status = "";
                    break;
                case "5":
                    joinPeopleData.status = "";
                    break;
                case "6":
                    joinPeopleData.status = "";
                    break;
            }
            joinPeopleData.time = item.winningTime;
            joinPeopleData.userHeadImag = item.userImage;

            joinPeopleDatas.add(joinPeopleData);
        }


        joinPeoplesDialog = JoinPeoplesDialog.getJoinPeoplesDialog(winner.participateUser.size() + "/" + winner.participateUser.size(), joinPeopleDatas);

    }

    @Override
    public void awardEnsure() {
        linearEnsure.setEnabled(false);
        MoHuToast.showToast("领取成功");
        finish();
    }

    @Override
    public void finish(){
        super.finish();
        EventBus.getDefault().post(new OrderFragmentEvenBus());
    }
}
